package com.eqdd.yiqidian.ui.yiqixue.fragment;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.eqdd.yiqidian.LearnMainFragmentCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.LearnArticleListBean;
import com.eqdd.yiqidian.bean.rebuild.LearnCommentTeacherListBean;
import com.eqdd.yiqidian.bean.rebuild.LearnCommentCourseListBean;
import com.eqdd.yiqidian.bean.rebuild.LearnCommentVideoListBean;
import com.eqdd.yiqidian.bean.rebuild.LearnLectureListBean;
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration;
import com.eqdd.yiqidian.box.span.MovementMethod.ClickMovementMethod;
import com.eqdd.yiqidian.box.span.TagSpan;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.ui.YoukuPlayActivity;
import com.eqdd.yiqidian.ui.yiqixue.LearnSearchActivity;
import com.eqdd.yiqidian.ui.yiqixue.info.LearnTeacherInfoActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 18-2-7  下午2:12.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class LearnMainFragment extends BaseFragment {
    private LearnMainFragmentCustom dataBinding;
    private SlimAdapterEx slimAdapterEx;

    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_learn_main, null, false);
    }

    @Override
    protected void initData() {
        if (slimAdapterEx == null) {
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 6);
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return 6;
                }
            });
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            dataBinding.recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity()));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_115, new SlimInjector<LearnCommentCourseListBean>() {
                @Override
                public void onInject(LearnCommentCourseListBean data, IViewInjector injector) {
                    injector.with(R.id.tv_one, view -> {
                        TextView textView = (TextView) view;
                        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("[").append(data.getLectureName()).append("]").append(data.getLectureName());
                        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.GREEN);
                        spannableStringBuilder.setSpan(foregroundColorSpan, 0, data.getLectureName().length() + 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                        textView.setText(spannableStringBuilder);
                    })
                            .with(R.id.tv_two, view -> {
                                TextView textView = (TextView) view;
                                textView.setText("");
                                String[] split = data.getLectCourseType().split(",");
                                for (String s : split) {
                                    SpannableString spanString = new SpannableString(s);
                                    TagSpan tagSpan = new TagSpan(Color.GREEN, 10, 10);
                                    tagSpan.setTextClick(text -> {
                                        Bundle bundle = new Bundle();
                                        bundle.putInt(Config.POS, 1);
                                        bundle.putString(Config.FIELD, text.toString());
                                        startActivity(LearnSearchActivity.class, bundle);

                                    });
                                    spanString.setSpan(tagSpan, 0, s.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                                    textView.append(spanString);
                                }
                                textView.setMovementMethod(ClickMovementMethod.getInstance());
                            })
                            .gone(R.id.tv_three);
//                            .text(R.id.tv_three, data.get() + "天(" + (6 * Integer.parseInt(data.getCourseTimes())) + "小时)  " + data.getCreateTime());
                }
            }).register(R.layout.list_item_112, new SlimInjector<LearnCommentVideoListBean>() {
                @Override
                public void onInject(LearnCommentVideoListBean data, IViewInjector injector) {
                    injector.image(R.id.iv_poster, data.getLectVideoImage())
                            .text(R.id.tv_title, data.getLectVideoTitle())
                            .with(R.id.tv_types, view -> {
                                TextView textView = (TextView) view;
                                textView.setText("");
                                String[] split = data.getLectVideoType().split(",");
                                for (String s : split) {
                                    SpannableString spanString = new SpannableString(s);
                                    TagSpan tagSpan = new TagSpan(Color.GREEN, 10, 10);
                                    tagSpan.setTextClick(text -> {
                                        Bundle bundle = new Bundle();
                                        bundle.putInt(Config.POS, 2);
                                        bundle.putString(Config.FIELD, text.toString());
                                        startActivity(LearnSearchActivity.class, bundle);

                                    });
                                    spanString.setSpan(tagSpan, 0, s.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                                    textView.append(spanString);
                                }
                                textView.setMovementMethod(ClickMovementMethod.getInstance());
                            });
                }
            }).register(R.layout.list_item_114, new SlimInjector<LearnCommentTeacherListBean>() {
                @Override
                public void onInject(LearnCommentTeacherListBean data, IViewInjector injector) {
                    injector.image(R.id.iv_cover, data.getLectureImage())
                            .text(R.id.tv_one, data.getLectureName())
                            .with(R.id.tv_two, view -> {
                                TextView textView = (TextView) view;
                                textView.setText("");
                                String[] split = data.getLectureType().split(",");
                                for (String s : split) {
                                    SpannableString spanString = new SpannableString(s);
                                    TagSpan tagSpan = new TagSpan(Color.GREEN, 10, 10);
                                    tagSpan.setTextClick(text -> {
                                        Bundle bundle = new Bundle();
                                        bundle.putInt(Config.POS, 0);
                                        bundle.putString(Config.FIELD, text.toString());
                                        startActivity(LearnSearchActivity.class, bundle);
                                    });
                                    spanString.setSpan(tagSpan, 0, s.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                                    textView.append(spanString);
                                }
                                textView.setMovementMethod(new ClickMovementMethod());
                            })
                            .text(R.id.tv_three, data.getLectureIntroduce())
                            .text(R.id.tv_four, data.getCreateTime());
                }
            }).register(R.layout.list_item_01, new SlimInjector<String>() {
                @Override
                public void onInject(String data, IViewInjector injector) {
                    injector.text(R.id.tv_name, data);
                }
            }).attachTo(dataBinding.recyclerView).updateData(new ArrayList<>());
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener((recyclerView, position, v) -> {
                       if(slimAdapterEx.getDataItem(position) instanceof LearnCommentTeacherListBean){
                           LearnCommentTeacherListBean dataItem = (LearnCommentTeacherListBean) slimAdapterEx.getDataItem(position);
                           Bundle bundle = new Bundle();
                           bundle.putString(Config.GUID,dataItem.getLectureGuid());
                           startActivity(LearnTeacherInfoActivity.class,bundle);
                       }
                    });
        }
    }

    @Override
    protected void setView() {
        Flowable<HttpResult<List<LearnCommentCourseListBean>>> lectureFlow = OkGo.<HttpResult<List<LearnCommentCourseListBean>>>post(HttpConfig.BASE_URL + HttpConfig.LEARN_LECTURE_COMMENT_LIST)
                .converter(new JsonConverter<HttpResult<List<LearnCommentCourseListBean>>>() {
                    @Override
                    public void test() {
                        super.test();
                    }
                })
                .adapt(new FlowableBody<>());
        Flowable<HttpResult<List<LearnCommentVideoListBean>>> videoFlow = OkGo.<HttpResult<List<LearnCommentVideoListBean>>>post(HttpConfig.BASE_URL + HttpConfig.LEARN_VIDEO_COMMENT_LIST)
                .converter(new JsonConverter<HttpResult<List<LearnCommentVideoListBean>>>() {
                    @Override
                    public void test() {
                        super.test();
                    }
                })
                .adapt(new FlowableBody<>());
        Flowable<HttpResult<List<LearnCommentTeacherListBean>>> teacherFlow = OkGo.<HttpResult<List<LearnCommentTeacherListBean>>>post(HttpConfig.BASE_URL + HttpConfig.LEARN_TEACHER_COMMENT_LIST)
                .converter(new JsonConverter<HttpResult<List<LearnCommentTeacherListBean>>>() {
                    @Override
                    public void test() {
                        super.test();
                    }
                })
                .adapt(new FlowableBody<>());

        Flowable.combineLatest(lectureFlow, videoFlow, teacherFlow, (listHttpResult, listHttpResult2, listHttpResult3) -> {
            List data = slimAdapterEx.getData();
            data.clear();
            data.add("讲师");
            data.addAll(listHttpResult3.getItems());
            data.add("课程");
            data.addAll(listHttpResult.getItems());
            data.add("视频");
            data.addAll(listHttpResult2.getItems());
            return data;
        }).subscribe(o -> slimAdapterEx.updateData(o));

    }

    @Override
    public void onClick(View v) {

    }


}
